<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Khanh
 * Date: 11/16/14
 * Time: 3:14 AM
 */
class Layout {

    private $ci;
    private $title_for_layout;
    private $meta_tags;
    private $layout;

    public function __construct() {

        $this->ci = &get_instance();
        $this->title_for_layout = NULL;
        $this->layout = 'default';
        $this->meta_tags = '';

    }

    public function loadLayout($layout) {
        $this->layout = $layout;
    }

    public function set_title($title = NULL) {
        $this->title_for_layout = $title;
    }

    public function add_meta_tags($name, $content) {
        $meta = '<meta name="'.$name.'" content="'.$content.'" />'."\n";
        $this->extra_head($meta);
        return $this;
    }

    public function extra_head()
    {
        $lines =& func_get_args();
        if(count($lines) > 0)
        {
            foreach($lines as $line)
            {
                $this->meta_tags .= "\t\t".$line."\n";
            }
        }
        return $this;
    }

    public function view($name, $data = array()) {

        $view = $this->ci->load->view($name, $data, TRUE);
        $title = '';
        if($this->title_for_layout !== NULL) {
            $title = $this->title_for_layout;
        }
        $this->ci->load->view('template/' . $this->layout, array(
                'title' => $title,
                'meta_data' => $this->meta_tags,
                'content' => $view
            ));
    }
}
?>